//
//  main.c
//  C_Study
//
//  Created by rui on 12-6-30.
//  Copyright (c) 2012年 Wang Rui. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <unistd.h> 

/*
 @要点：数组
 */

typedef unsigned int bool;
#define true 1;
#define false 0;

#undef DEBUG
#define DEBUG 1


struct Array {
	int *pBase;//真正数组的起始地址
	int length;
	int ctn;//当前有存入效个数
};
typedef struct Array Array;


bool is_empty(Array *pArr){
	assert(NULL != pArr);
	if (pArr->ctn == 0) {
		return true;
	} else {
		return false;
	}
}

bool is_full(Array *pArr){
	assert(NULL != pArr);
	if (pArr->ctn == pArr->length) {
		return true;
	} else {
		return false;
	}
}

//print array
void show_arr(Array *pArr){
	assert(NULL != pArr);
	if (is_empty(pArr)) 
	{
		printf("Array is empty!\n");
	} 
	else 
	{
		printf("Exsiting Array:");
		for (int i=0; i<pArr->ctn; i++) {
			printf("%d ",pArr->pBase[i]);
		}
		printf("\n");
	}
}

//初始化
void init_arr(Array *pArr,int length){
	assert(NULL != pArr);
	pArr->pBase = (int *)malloc(sizeof(int)*length);
	
	if (pArr == NULL) 
	{
		printf("malloc failed!\n");
		exit(0);
	} 
	else 
	{
		pArr->length = length;
		pArr->ctn = 0;
	}
}

//末尾增加元素
bool append(Array *pArr,int num){
	assert(NULL != pArr);
	if (is_full(pArr)) 
	{
		printf("Array is full,Can't append!\n");
		return false;
	}
	else 
	{
		pArr->pBase[pArr->ctn] = num;
		pArr->ctn ++;
	}
	return true;
}

bool insert(Array *pArr,int pos,int val){
	assert(NULL != pArr);
	if (is_full(pArr)) 
	{
		printf("Array is full,Can't insert!\n");
		return false;
	}
	else if(pos < 1 || pos > pArr->ctn+1){
		printf("Insert Beyond bound!\n");
		return false;
	}
	else 
	{
		//先移动
		for (int i=pArr->ctn-1; i>=pos-1; i--) {
			pArr->pBase[i+1] = pArr->pBase[i];
		}
		
		pArr->pBase[pos-1] = val;
		pArr->ctn ++;
	}
	return true;
}

bool delete(Array *pArr,int pos,int *delVal){
	assert(NULL != pArr);
	if (is_empty(pArr)) 
	{
		printf("Array is empty,Can't delete!\n");
		return false;
	}
	else if (pos<1 || pos>pArr->ctn) 
	{
		printf("Delete Beyond bound!\n");
		return false;
	}
	else 
	{
		*delVal = pArr->pBase[pos-1];
		for (int i=pos; i<=pArr->ctn-1; i++) 
		{
			pArr->pBase[i-1] = pArr->pBase[i];
		}
		pArr->ctn --;
	}
	return true;
}

void reverse_arr(Array *pArr){
	assert(NULL != pArr);
	if (is_empty(pArr)) 
	{
		printf("Array is empty,Can't reverse!\n");
		return;
	}
	else 
	{
		int t,i=0;
		int j=pArr->ctn-1;
		while (i != j) {
			t = pArr->pBase[i];
			pArr->pBase[i] = pArr->pBase[j];
			pArr->pBase[j] = t;
			i++;
			j--;
		}
	}
}

void sort_arr(Array *pArr){
	assert(NULL != pArr);
	if (is_empty(pArr)) 
	{
		printf("Array is empty,Can't sort!\n");
		return;
	}
	
	//use boob sort
	for (int i = 1; i<pArr->ctn; i++) {
		for (int j = 0; j<pArr->ctn-i; j++) {
			if (pArr->pBase[j] > pArr->pBase[j+1]) {
				int t = pArr->pBase[j];
				pArr->pBase[j] = pArr->pBase[j+1];
				pArr->pBase[j+1] = t;
			}
		}
	}
}


/**********************************
 *@ main函数
 **********************************/
int main(int argc, const char * argv[])
{
	Array arr;
	init_arr(&arr,10);
	
	append(&arr, 10);
	append(&arr, 12);
	append(&arr, 5);
	append(&arr, 17);
	append(&arr, 3);
	insert(&arr, 2, 345);
	
	show_arr(&arr);
	
	int del;
	delete(&arr, 2, &del);
	printf("delete Num:%d\n",del);
	show_arr(&arr);
	
	sort_arr(&arr);
	show_arr(&arr);
	
	reverse_arr(&arr);
	show_arr(&arr);
	
	append(&arr, 3);
	append(&arr, 8);
	append(&arr, 23);
	append(&arr, 4);
	append(&arr, 67);
	sort_arr(&arr);
	show_arr(&arr);
	
	return 0;
}

